<template>
    <a-form-model ref="form" :model="form" layout="vertical" class="rx-a-form-vertical">
        <a-form-model-item v-if="form.type=='dictionary'" :label="i18n('form.matchDic','匹配字典')"  >
            <rx-input-button v-model="dicKeyTitle"
                             @click="selectDic">
            </rx-input-button>
        </a-form-model-item>

    </a-form-model>
</template>

<script>

import SysTreeApi from "@/api/system/core/sysTree";
import { Dialog,  Util, App,JsConstant} from "@lowcode/jpaas-common-lib"




export default {
    name: "renderConfig_dicRender",
    props: ["layerid", "destroy"],
    mixins:[App],
    computed: {
        table() {
            return this.$refs.table;
        },
        tableConf() {
            return this.$refs.tableConf;
        },
    },
    created() {

    },
    data() {
        return {
            labelCol: {span: 6},
            wrapperCol: {span: 18},
            dicTree: [],
            form: {
                //table:表,dictionary:数据字典
                type: "dictionary",
                //字典
                dicKey: "",
                dicName:"",
            },
            dicKeyTitle:"",
        }
    },
    methods: {
        i18n(name,text,key){
            return this.$ti18(name,text,"formCellRender",key);
        },
        setData(data, fields) {
            if (data.type) {
                this.form = data;
            }
            if(data.dicKey){
                if(data.dicName){
                    let dicKeyTitle = {
                        label:data.dicName,
                        text:data.dicName,
                        value:data.dicKey
                    }
                    this.dicKeyTitle=JSON.stringify(dicKeyTitle)
                    this.form.dicKey = data.dicKey
                    this.form.dicName = data.dicName
                    //this.setVal(this.form.dicKey,this.form.dicName);
                    return;
                }
                var self_ = this;
                SysTreeApi.listDicTree(this.curApp.appId).then(res => {
                    var cloneData = [];
                    res.forEach(function (item) {
                        cloneData.push({
                            title: item.name,
                            key: item.treeId,
                            value: item.alias,
                            parentId: item.parentId
                        })
                    })
                    var treeData = Util.listToTree(cloneData, "key", "parentId");
                    self_.dicTree = treeData;

                    for (var i = 0; i < self_.dicTree.length; i++) {
                        var item = self_.dicTree[i];
                        //检索出当前选中的数据字典，保存名称和别名
                        if (item.key == data.dicKey) {
                            self_.form.dicKey = item.value;
                            self_.form.dicName = item.title;
                            self_.dicKeyTitle = '{value:\'' + item.value + '\',text:\'' + item.title + '\'}';
                            break;
                        }
                    }
                })

            }
        },
        selectDic(vm){
            var conf = {
                curVm: this, data: {isFull:true}, widthHeight: ['800px', '600px'],
            }
            let self_ = this;
            Dialog.openSysDicDialog(conf,function (curVue, row) {
                self_.form.dicKey = row.pkId || row.treeId;
                self_.form.dicName = self_.$i18(row.name,"名称")
                vm.setVal(self_.form.dicKey,self_.form.dicName);
            })
        },
        validate(callback) {
            var self = this;
            callback();
        },
        getData() {
            return this.form;
        },
        loadDicTree() {
            SysTreeApi.listDicTree(this.curApp.appId).then(res => {
                var cloneData = [];
                res.forEach(function (item) {
                    cloneData.push({
                        title: item.name,
                        key: item.treeId,
                        value: item.alias,
                        parentId: item.parentId
                    })
                })
                var treeData = Util.listToTree(cloneData, "key", "parentId");
                this.dicTree = treeData;
            })
        },
    }
}
</script>

<style scoped>

</style>